'**************************************************************************
'*                PC-FXGA Tool Kit セットアップ 
'**************************************************************************

'' '$DEFINE DEBUG  	''Define for script development/debugging
'$DEFINE VCD_DRIVER	''Define for DirverI/O mode debugger

'$INCLUDE 'setupapi.inc'                        
'$INCLUDE 'msdetect.inc'
'$INCLUDE 'msshared.inc'

''Dialog ID's
CONST REBOOT        = 2
CONST VU_DRV_CAUTION= 4
CONST WELCOME       = 100	''起動後最初に開くダイアログ
CONST ASKQUIT       = 200	''インストールしないで中断のときのダイアログ
CONST DESTPATH      = 300	''インストール先の指定ダイアログ
CONST EXITFAILURE   = 400	''
CONST EXITQUIT      = 600	''
CONST EXITSUCCESS   = 700
CONST OPTIONS       = 800
CONST APPHELP       = 900
CONST BADPATH       = 6400
CONST BILLBOARD     = 4545

''Bitmap ID
CONST LOGO 			= 1		''会社名ロゴのビットマップ（２色）

GLOBAL DEST$        ''Default destination directory.
GLOBAL OPTCUR$      ''Option selection from option dialog.
GLOBAL CUIDLL$      ''Name of CustomUserInterface DLL 
GLOBAL DRVINST%


DECLARE SUB Install

DECLARE FUNCTION MakePath (szDir$, szFile$) AS STRING
DECLARE FUNCTION MakePath2 (szDir$, szFile$) AS STRING


'$IFDEF CUSTOM_MENU
DECLARE SUB Custom_Install
DECLARE SUB SetDriveStatus
'$ENDIF ''

INIT:
	CUIDLL$ = "mscuistf.dll"            ''Custom user interface dll
	HELPPROC$ = "FHelpDlgProc"          ''Help dialog procedure

	SetBitmap CUIDLL$, LOGO
	SetTitle "ＧＭＡＫＥＲ セットアップ"

	szInf$ = GetSymbolValue("STF_SRCINFPATH")
	IF szInf$ = "" THEN
		szInf$ = GetSymbolValue("STF_CWDDIR") + "setup.inf"
	END IF
	WinDrive$ = MID$(GetWindowsDir, 1, 1)
	ReadInfFile szInf$
	
					   
	DRVINST%  = 0
	DRIVER_F% = 0
	OPTCUR$ = "1"
	DEST$ = WinDrive$+":\FXGA\WIN"

	i% = SetSizeCheckMode(scmOnIgnore)    '' could use scmOff; def = scmOnFatal
	IF IsDriveValid(WinDrive$) = 0 THEN
		i% = DoMsgBox("Windows ドライブ ('"+WinDrive$+"') は、無効なドライブです。", "DEBUG", MB_TASKMODAL+MB_ICONHAND+MB_OK)
		GOTO QUIT
	END IF

WELCOME:
	sz$ = UIStartDlg(CUIDLL$, WELCOME, "FInfoDlgProc", APPHELP, HELPPROC$)
	IF sz$ = "CONTINUE" THEN
		UIPop 1
	ELSEIF sz$ = "EXIT" THEN
		UIPopAll
		ERROR STFQUIT
	ELSE
		GOSUB WELCOME
	END IF



GETPATH:
	SetSymbolValue "EditTextIn", DEST$
	SetSymbolValue "EditFocus", "END"
GETPATHL1:
	sz$ = UIStartDlg(CUIDLL$, DESTPATH, "FEditDlgProc", APPHELP, HELPPROC$)
	DEST$ = GetSymbolValue("EditTextOut")

	IF sz$ = "CONTINUE" THEN
		IF IsDirWritable(DEST$) = 0 THEN
			GOSUB BADPATH
			GOTO GETPATHL1
		END IF
		UIPop 1
	ELSEIF sz$ = "REACTIVATE" THEN
		GOTO GETPATHL1
	ELSEIF sz$ = "BACK" THEN
		UIPop 1
		GOTO WELCOME
	ELSE
		GOSUB ASKQUIT
		GOTO GETPATH
	END IF

	SetRestartDir DEST$

	Install

	Res% = RestartListEmpty()
	IF Res% <> 1 THEN 
REBOOT_MES:
		sz$ = UIStartDlg(CUIDLL$, REBOOT, "FInfoDlgProc", APPHELP, HELPPROC$)
		IF sz$ = "CONTINUE" THEN
			UIPop 1
		ELSE
			GOTO REBOOT_MES
		END IF
		i% = ExitExecRestart()
		GOTO REBOOT_MES
	END IF

QUIT:
	ON ERROR GOTO ERRQUIT

	IF ERR = 0 THEN
		IF DRVINST% = 0 THEN
			dlg% = EXITSUCCESS
		ELSE
			dlg% = VU_DRV_CAUTION
		ENDIF
	ELSEIF ERR = STFQUIT THEN
		dlg% = EXITQUIT
	ELSE
		dlg% = EXITFAILURE
	END IF
QUITL1:
	sz$ = UIStartDlg(CUIDLL$, dlg%, "FInfo0DlgProc", 0, "")
	IF sz$ = "REACTIVATE" THEN
		GOTO QUITL1
	END IF
	UIPop 1
	IF DRVINST% = 1 THEN
		i% = ExitExecRestart()
	END IF

	END

ERRQUIT:
	i% = DoMsgBox("セットアップのファイルが破損しているおそれがあります。", "セットアップ メッセージ", MB_OK+MB_TASKMODAL+MB_ICONHAND)
	END



BADPATH:
	sz$ = UIStartDlg(CUIDLL$, BADPATH, "FInfo0DlgProc", 0, "")
	IF sz$ = "REACTIVATE" THEN
		GOTO BADPATH
	END IF
	UIPop 1
	RETURN



ASKQUIT:
	sz$ = UIStartDlg(CUIDLL$, ASKQUIT, "FQuitDlgProc", 0, "")

	IF sz$ = "EXIT" THEN
		UIPopAll
		ERROR STFQUIT
	ELSEIF sz$ = "REACTIVATE" THEN
		GOTO ASKQUIT
	ELSE
		UIPop 1
	END IF
	RETURN



'**
'** Purpose:
'**     Builds the copy list and performs all installation operations.
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
SUB Install STATIC

''**** Install Files *****
	SrcDir$ = GetSymbolValue("STF_SRCDIR")
	AGEDIR$ = MakePath(DEST$,"AGE")
	ROOT$   = DEST$

'' Appulication File

	CreateDir AGEDIR$, cmoNone
	ClearCopyList
	AddSectionFilesToCopyList "AGE", SrcDir$, AGEDIR$
	AddSectionFilesToCopyList "FILES",    SrcDir$, ROOT$

 	SetCopyGaugePosition 96, 64
	CopyFilesInCopyList

''**** Entry Program manageor *****
	GRP$="PC-FXGA TOOLS"
	CreateProgmanGroup GRP$, "", cmoNone
	ShowProgmanGroup   GRP$, 1, cmoNone

	CreateProgmanItem  GRP$,"AGE",MakePath(AGEDIR,"age.exe"),"",cmoOverwrite
	CreateProgmanItem  GRP$," AGE HELP","winhelp "+MakePath(AGEDIR,"age.hlp"),"",cmoOverwrite


END SUB
''
''
''
''
''

'**
'** Purpose:
'**     Appends a file name to the end of a directory path,
'**     inserting a backslash character as needed.
'** Arguments:
'**     szDir$  - full directory path (with optional ending "\")
'**     szFile$ - filename to append to directory
'** Returns:
'**     Resulting fully qualified path name.
'*************************************************************************
FUNCTION MakePath (szDir$, szFile$) STATIC AS STRING
	IF szDir$ = "" THEN
		MakePath = szFile$
	ELSEIF szFile$ = "" THEN
		MakePath = szDir$
	ELSEIF MID$(szDir$, LEN(szDir$), 1) = "\" THEN
		MakePath = MakePath2(szDir$, szFile$)
	ELSE
		MakePath = szDir$ + "\" + szFile$
	END IF
END FUNCTION


'**
'** Purpose:
'**     If ending "\" is a tailbyte of a DBCS character, append a "\" 
'**     to the end of the file name.
'** Arguments:
'**     szDir$  - full directory path (with optional ending "\")
'**     szFile$ - filename to append to directory
'** Returns:
'**     Resulting fully qualified path name.
'*************************************************************************
FUNCTION MakePath2 (szDir$, szFile$) STATIC AS STRING
	IF IsDBCSChar(szDir$, LEN(szDir$)) = 2 THEN
		MakePath2 = szDir$ + "\" + szFile$
	ELSE
		MakePath2 = szDir$ + szFile$
	END IF
END FUNCTION



'$IFDEF CUSTOM_MENU

SUB Custom_Install STATIC
CUSTINST:
	sz$ = UIStartDlg(CUIDLL$, 3, "FCustInstDlgProc", APPHELP, HELPPROC$)

	IF sz$ = "CONTINUE" THEN
		''Install only if it will fit.
		FOR i% = 1 TO 3 STEP 1
			IF GetListItem(BIGLIST$, i%) <> "" THEN
''				GOSUB TOOBIG
				GOTO CUSTINST
			END IF
		NEXT i%
		UIPop 1
''		GOTO INSTALL
		RETURN
	ELSEIF sz$ = "PATH" THEN
		i% = DoMsgBox("PATH", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
		GOTO CUSTINST
	ELSEIF sz$ = "CHK1" THEN
		i% = DoMsgBox("CHK1", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
''		RecalcOptFiles APPFILES
''		SetDriveStatus
		GOTO CUSTINST
	ELSEIF sz$ = "CHK2" THEN
		i% = DoMsgBox("CHK2", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
''		RecalcOptFiles OPTFILES1
''		SetDriveStatus
		GOTO CUSTINST
	ELSEIF sz$ = "CHK3" THEN
		i% = DoMsgBox("CHK3", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
''		RecalcOptFiles OPTFILES2
''		SetDriveStatus
		GOTO CUSTINST
	ELSEIF sz$ = "BTN2" THEN
		i% = DoMsgBox("BTN2", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
''		GOTO OPTFILES1
	ELSEIF sz$ = "BTN3" THEN
		i% = DoMsgBox("BTN3", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
	ELSEIF sz$ = "REACTIVATE" THEN
		i% = DoMsgBox("REACTIVE", "DEBUG", MB_OK+MB_TASKMODAL+MB_ICONHAND)
''		RecalcPath
''		SetDriveStatus
		GOTO CUSTINST
	ELSE
''		GOSUB ASKQUIT
		GOTO CUSTINST
	END IF

	
END SUB

'**
'** Purpose:
'**     Sets drive status info according to latest disk space calcs.
'** Arguments:
'**     none.
'** Returns:
'**     none.
'*************************************************************************
SUB SetDriveStatus STATIC

    drive$ = MID$(DEST$, 1, 1)
    ndrive% = ASC(ucase$(drive$)) - ASC("A") + 1
    cost& = VAL(GetListItem(APPNEEDS$, ndrive%)) + VAL(GetListItem(OPT1NEEDS$, ndrive%)) + VAL(GetListItem(OPT2NEEDS$, ndrive%))
    free& = GetFreeSpaceForDrive(drive$)
    ReplaceListItem DRIVETEXT$, 1, drive$ + ":"
    ReplaceListItem DRIVETEXT$, 2, STR$(cost& / 1024) + " K"
    ReplaceListItem DRIVETEXT$, 3, STR$(free& / 1024) + " K"

    IF drive$ = WINDRIVE$ THEN
        ReplaceListItem DRIVETEXT$, 4, ""
        ReplaceListItem DRIVETEXT$, 5, ""
        ReplaceListItem DRIVETEXT$, 6, ""
    ELSE
        ndrive% = ASC(ucase$(WINDRIVE$)) - ASC("A") + 1
        cost& = VAL(GetListItem(APPNEEDS$, ndrive%)) + VAL(GetListItem(OPT1NEEDS$, ndrive%)) + VAL(GetListItem(OPT2NEEDS$, ndrive%))
        IF cost& = 0 THEN
            ReplaceListItem DRIVETEXT$, 4, ""
            ReplaceListItem DRIVETEXT$, 5, ""
            ReplaceListItem DRIVETEXT$, 6, ""
        ELSE
            free& = GetFreeSpaceForDrive(WINDRIVE$)
            ReplaceListItem DRIVETEXT$, 4, WINDRIVE$ + ":"
            ReplaceListItem DRIVETEXT$, 5, STR$(cost& / 1024) + " K"
            ReplaceListItem DRIVETEXT$, 6, STR$(free& / 1024) + " K"
        END IF
    END IF
END SUB


'$ENDIF ''CUSTOM_MENU
